<?php

namespace App\Http\Controllers\Activity\Web;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\FeishuWebAuth;
use App\Models\Activity\ActivityModel;
use App\Models\Activity\ActivityShopJoinModel;
use App\Service\Activity\ActivityAuditService;
use App\Service\BaseService;
use App\Service\Cruise\Mission\ActivityInputService;
use Illuminate\Http\Request;

class ActivityAuditController extends BaseController
{
    public function enums(Request $request)
    {
        $data = [
            'activityType' => enumToSelection(ActivityModel::ACTIVITY_TYPE_TEXT),
            'auditStatus'  => enumToSelection(ActivityShopJoinModel::STATUS_TEXT),
        ];

        return $this->success($data);
    }

    public function lists(Request $request)
    {
        $this->validate($request, $rules = [
            'activityName'          => 'string',
            'activityType'          => 'integer',
            'activityStartDateTime' => 'date_format:Y-m-d',
            'activityEndDateTime'   => 'date_format:Y-m-d',
            'shopIds'               => 'string',
            'shopCodes'             => 'string',
            'actionName'            => 'string',
            'submitUid'             => 'integer',
            'submitMobile'          => 'string',
            'auditStatus'           => 'integer',
            'auditUid'              => 'integer',
            'remark'                => 'string',
            'pageSize'              => 'integer',
            'pageNum'               => 'integer',
        ]);

        $data = [];
        foreach ($rules as $field => $rule) {

            if ($rule == 'integer') {
                $default = null;
            } elseif ($rule == 'array') {
                $default = [];
            } else {
                $default = '';
            }

            $data[$field] = $request->get($field, $default);
        }

        $data['pageSize'] = $request->get('pageSize', 15);
        $data['pageNum']  = $request->get('pageNum', 1);

        $data = ActivityAuditService::lists($data);

        return $this->success($data);
    }

    public function detail(Request $request, int $id)
    {
        $data = ActivityAuditService::detail($id, FeishuWebAuth::$userId);

        return $this->success($data);
    }

    public function agree(Request $request, int $id)
    {
        $this->validate($request, [
            'reason' => 'required|string',
            'agree'  => 'required|in:1,2',
        ]);

        $cruiseOrderId = ActivityShopJoinModel::query()->where('id', $id)->value('orderNo');
        if (!$cruiseOrderId) {
            BaseService::throwErr('数据不存在');
        }

        (new ActivityInputService($cruiseOrderId, FeishuWebAuth::$userId))->audit([
            'reason' => $request->get('reason'),
            'agree'  => $request->get('agree'),
        ]);

        return $this->success();
    }
}
